{include file="public/nheader"}
<div class="layui-fluid">
    <div class="layui-card">
        <!-- // 默认操作按钮 -->
        <div class="layui-card-header layadmin-card-header-auto ">
            <div class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <select name="status" lay-verify="">
                                <option value="">状态</option>
                                <option value="1" {if !empty(input('get.status')) &&
                                    input('get.status')==1}selected{/if}>正常</option>
                                <option value="2" {if !empty(input('get.status')) &&
                                    input('get.status')==2}selected{/if}>冻结</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="type" lay-verify="">
                                <option value="">代理类型</option>
                                <option value="2" {if !empty(input('get.type')) && input('get.type')==2}selected{/if}>
                                    城市代理</option>
                                <option value="3" {if !empty(input('get.type')) && input('get.type')==3}selected{/if}>
                                    区域代理</option>
                                <option value="4" {if !empty(input('get.type')) && input('get.type')==4}selected{/if}>
                                    街道代理</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <select name="top" lay-filter="top">
                                <option value="">推荐合伙人</option>
                                {foreach $agent as $v}
                                <option value="{$v.id}" {if !empty(input('get.top')) &&
                                    input('get.top')==$v.id}selected{/if}>{$v.nickname}({$v.accounts})</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <select name="province" lay-filter="province">
                                <option value="">请选择搜索省份</option>
                                {foreach $province as $v}
                                <option value="{$v.area_code}" {if !empty(input('get.province')) &&
                                    input('get.province')==$v.area_code}selected{/if}>{$v.name}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <select name="city" lay-filter="city">
                                <option value="">请选择搜索城市</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <select name="area" lay-filter="area">
                                <option value="">请选择搜索区域</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <select name="street" lay-filter="street">
                                <option value="">请选择搜索街道</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline ">
                            <input name="title" class="layui-input" type="text" placeholder="备注|账号|手机号" />
                        </div>
                    </div>

                    <div class="layui-inline">
                        <!-- // 默认搜索 -->
                        <button class="layui-btn icon-btn" lay-filter="formSearch" lay-submit>搜索</button>

                        <!-- // 打开添加页面 -->
                        <button class="layui-btn icon-btn" lay-maxmin="true" lay-open="" data-title="添加"
                            data-area="60%,80%" data-url="/agent/agent_edit">添加
                        </button>
                        <!-- // 删除所有 -->
                        <!-- <button class="layui-btn icon-btn layui-btn-danger" lay-batch="lay-tableList"
                            data-url="/agent/agent_del">批量删除
                        </button> -->
                    </div>
                </div>
            </div>
        </div>

        <!-- // 创建数据实例 -->
        <table id="lay-tableList" lay-filter="lay-tableList"></table>
    </div>
</div>

<script type="text/html" id="datagrid-toolbar">
</script>
<!-- // 列表状态栏 -->
<script type="text/html" id="columnStatus">
     <input type="checkbox" lay-filter="switchStatus" data-url="/agent/agent_status?id={{d.id}}&field=status" lay-skin="switch"
     {{d.status==1?'checked':''}}   lay-text="正常|冻结"   />
</script>

<!-- // 列表工具栏 -->
<script type="text/html" id="tableBar">
    <a class="layui-table-text" lay-open data-title="编辑" data-area="60%,80%" data-url="/agent/agent_edit?id={{d.id}}" >编辑</a>
    {{#  if(d.id !=1){ }}
    <div class="layui-divider layui-divider-vertical"></div>
    <a class="layui-table-text" data-url="/agent/agent_del?id={{d.id}}" data-title=" 执行该操作后代理区域和绑定师傅街道会删除" lay-event="del">删除</a>
    {{#  } }}
</script>
<script type="text/html" id="tableBar1">
    <a class="layui-table-text" lay-open data-title="管理区域" data-area="80%,90%" data-url="/agent/agentarea?agentid={{d.id}}" >查看</a>
</script>
<script type="text/html" id="tableBar2">
    <a class="layui-table-text" lay-open data-title="类别分类" data-area="80%,90%" data-url="/agent/subcate?agentid={{d.id}}" >查看</a>
</script>
<script type="text/html" id="tableBar3">
    <a class="layui-table-text" lay-open data-title="产品管理" data-area="80%,90%" data-url="/agent/product?agentid={{d.id}}" >查看({{d.product}})</a>
</script>
<!-- //money修改 -->
<script type="text/html" id="adminforms">
    <div class="layui-fluid layui-bg-white">
        <form class="layui-form layui-form-fixed" lay-filter="adminforms">
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">* </font>账号:</label>
                    <div class="layui-input-block">
                        <input name="accounts" style="border:none;display: inline-block;" class="layui-input" disabled onlyread/>
                    </div>
                </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">* </font>可用金额:</label>
                    <div class="layui-input-block">
                        <input name="money" style="border:none;display: inline-block;" class="layui-input" disabled onlyread/>
                    </div>
                </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">* </font>类型:</label>
                <div class="layui-input-block">
                    <input type="radio" name="type" value="1" title="增加资产" checked>
                    <input type="radio" name="type" value="2" title="减少资产">
                </div>
            </div>
            <div class="layui-form-item">
            <label class="layui-form-label"><font color="red">* </font>金额:</label>
                <div class="layui-input-block">
                    <input name="moneys" value=""  placeholder="增减金额" type="number" class="layui-input" lay-verify="required" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">* </font>操作说明:</label>
                <div class="layui-input-block">
                    <textarea name="desc" placeholder="请输入文本内容" class="layui-textarea" lay-verify="required" ></textarea>
                </div>
            </div>
        <div class="layui-footer layui-form-item layui-center ">
            <input type="hidden" value="1" name="types" />
            <button class="layui-btn" lay-filter="submitPage" lay-submit>提交</button>
            <button class="layui-btn layui-btn-primary" type="button" sa-event="closePageDialog" >取消</button>
        </div>
        </form>
    </div>
</script>
<!-- money2修改 -->
<script type="text/html" id="adminforms2">
    <div class="layui-fluid layui-bg-white">
        <form class="layui-form layui-form-fixed" lay-filter="adminforms2">
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">* </font>账号:</label>
                    <div class="layui-input-block">
                        <input name="accounts" style="border:none;display: inline-block;" class="layui-input" disabled onlyread/>
                    </div>
                </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">* </font>可用金额:</label>
                    <div class="layui-input-block">
                        <input name="money2" style="border:none;display: inline-block;" class="layui-input" disabled onlyread/>
                    </div>
                </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">* </font>类型:</label>
                <div class="layui-input-block">
                    <input type="radio" name="type" value="1" title="增加需缴纳额" checked>
                    <input type="radio" name="type" value="2" title="扣除需缴纳额">
                </div>
            </div>
            <div class="layui-form-item">
            <label class="layui-form-label"><font color="red">* </font>金额:</label>
                <div class="layui-input-block">
                    <input name="moneys" value=""  placeholder="增减金额" type="number" class="layui-input" lay-verify="required" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">* </font>操作说明:</label>
                <div class="layui-input-block">
                    <textarea name="desc" placeholder="请输入文本内容" class="layui-textarea" lay-verify="required" ></textarea>
                </div>
            </div>
        <div class="layui-footer layui-form-item layui-center ">
            <input type="hidden" value="2" name="types" />
            <button class="layui-btn" lay-filter="submitPage" lay-submit>提交</button>
            <button class="layui-btn layui-btn-primary" type="button" sa-event="closePageDialog" >取消</button>
        </div>
        </form>
    </div>
</script>
{include file="public/nfooter"}
<script>
    layui.use(['admin', 'layer', 'table', 'form'], function () {

        var admin = layui.admin;
        var layer = layui.layer;
        var table = layui.table;        // 表格
        var form = layui.form;
        admin.callback.edits = function (clickthis, colletction, config) {
            var tableThis = colletction.tableThis;
            // 监听权限提交
            form.on("submit(submitPage)", function (post) {
                // 获取用户id
                var pageThat = layui.$(this);
                pageThat.attr("disabled", true);
                // 开始POST提交数据
                post.field.id = tableThis.data.id;
                var index = layer.load(1); //换了种风格           
                layui.$.post("{:url('agent/money_edit')}", post.field, function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg);
                        // 关闭当前窗口
                        table.reload('lay-tableList');
                        layer.close(colletction.index);
                        layer.close(index);
                        pageThat.attr("disabled", false);
                    }
                    else {
                        layer.error(res.msg);
                        layer.close(index);
                        pageThat.attr("disabled", false);
                    }
                }, 'json');
                return false;
            })
        }
        admin.callback.edits2 = function (clickthis, colletction, config) {
            var tableThis = colletction.tableThis;
            // 监听权限提交
            form.on("submit(submitPage)", function (post) {
                // 获取用户id
                var pageThat = layui.$(this);
                pageThat.attr("disabled", true);
                // 开始POST提交数据
                post.field.id = tableThis.data.id;
                var index = layer.load(1); //换了种风格           
                layui.$.post("{:url('agent/money_edit')}", post.field, function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg);
                        // 关闭当前窗口
                        table.reload('lay-tableList');
                        layer.close(colletction.index);
                        layer.close(index);
                        pageThat.attr("disabled", false);
                    }
                    else {
                        layer.error(res.msg);
                        layer.close(index);
                        pageThat.attr("disabled", false);
                    }
                }, 'json');
                return false;
            })
        }
        form.on("submit(formSearch)", function (post) {
            // 获取用户id
            if (post.field.province || post.field.city || post.field.area || post.field.street) {
                if (!post.field.type) {
                    layer.error('请填写合伙人类型');
                    return false;
                }
            }
            layui.$.get("{:url('')}", post.field, function (res) {
                if (res.code == 200) {
                    table.reload("lay-tableList", {
                        page: {
                            curr: 1
                        },
                        where: post.field
                    })
                }
                else {
                    layer.error(res.msg);
                }
            }, 'json');

            return false;
        })
        /*
         * 初始化表格
        */
        var isTable = table.render({
            elem: "#lay-tableList"
            , url: "{:url()}"
            , page: true
            , size: 'lg'
            , toolbar: 'true'
            // , defaultToolbar: ["filter","exports","print"]
            , defaultToolbar: [{
                title: '导出excel' //标题
                , layEvent: 'LAYTABLE_EXPORT' //事件名，用于 toolbar 事件中使用
                , icon: 'layui-icon-export' //图标类名
            }, {
                title: '打印页面' //标题
                , layEvent: 'LAYTABLE_PRINT' //事件名，用于 toolbar 事件中使用
                , icon: 'layui-icon-print' //图标类名
            }, {
                title: '筛选数据' //标题
                , layEvent: 'LAYTABLE_COLS' //事件名，用于 toolbar 事件中使用
                , icon: 'layui-icon-cols' //图标类名
            }]
            , even: true
            , cols: [[
                // { type: 'checkbox', width: 60 },
                { field: 'id', align: 'center', sort: true, width: 80, title: 'ID' },
                {
                    field: 'dr', align: 'center', width: 150, title: '导入类别、项目', templet: function (d) {

                        return '<a class="layui-badge layui-bg-blue"  data-reload="self" data-url="/agent/index_exc?id=' + d.id + '" lay-ajax="" data-confirm="一键导入将平台产品、分类导入到代理中,已导入产品、分类不会重复导入,数据越多导入时间将越长,请确定要进行此操作吗？">一键导入</a>';
                    }
                },
                { field: 'agentname', width: 200, align: 'center', title: '上级' },
                { align: 'center', width: 100, toolbar: '#tableBar1', title: '责任区域' },
                { align: 'center', width: 100, toolbar: '#tableBar2', title: '类别' },
                { align: 'center', width: 100, toolbar: '#tableBar3', title: '产品' },
                {
                    field: 'yyopen', align: 'center', width: 120, title: 'N元活动', templet: function (d) {
                        if (d.yyopen == 1) {
                            return '关闭';
                        } else {
                            return '开启';
                        }
                    }
                },
                // {
                //     field: 'openid', align: 'center', width: 120, title: '绑定微信', templet: function (d) {
                //         return d.openid ? '<a class="layui-badge layui-bg-blue" data-url="/agent/agent_status?id=' + d.id + '&field=openid" lay-ajax="" data-confirm data-reload="self">解绑</a>' : '<span class="layui-badge">未绑定</span>';
                //     }
                // },
                { field: 'accounts', width: 200, align: 'center', title: '账号' },
                { field: 'mobile', width: 200, align: 'center', title: '手机' },
                {
                    field: 'money', align: 'center', width: 200, templet: function (d) {
                        return '<a style="color:blue" class="layui-table-text" lay-open data-title="明细" data-area="60%,80%" data-url="/agent/agentlog?agentid=' + d.id + '&types=1" >' + d.money + '</a>&nbsp;<a class="layui-table-text" data-title="操作" data-area="30%,40%" callback="edits" data-url="#adminforms" lay-event="edit">修改</a>';
                    }, title: '余额'
                },
                {
                    field: 'money2', align: 'center', width: 120, templet: function (d) {
                        return '<a style="color:blue" class="layui-table-text" lay-open data-title="明细" data-area="60%,80%" data-url="/agent/agentlog?agentid=' + d.id + '&types=2" >' + d.money2 + '</a>&nbsp;<a class="layui-table-text" data-title="操作" data-area="30%,40%" callback="edits2" data-url="#adminforms2" lay-event="edit">修改</a>';
                    }, title: '需缴纳额'
                },
                {
                    field: 'money2', align: 'center', width: 200, templet: function (d) {
                        var moneyss = d.money2 - d.btmoneys;
                        if (moneyss < 0) {
                            moneyss = 0;
                        }
                        return moneyss.toFixed(2);
                    }, title: '扣除未使用券需缴纳'
                },
                {
                    field: 'btmoneys', align: 'center', width: 300, title: '发放卡已使用/进行中/未使用金额', templet: function (d) {
                        return d.btmoneys1 + '元/' + d.btmoneys2 + '元/' + d.btmoneys + '元';
                    }
                },
                {
                    field: 'money', align: 'center', width: 120, templet: function (d) {
                        return '<a style="color:blue" class="layui-table-text" lay-open data-title="订单明细" data-area="90%,90%" data-url="/orders/index?agentid=' + d.id + '" >查看</a>';
                    }, title: '订单'
                },
                { field: 'nickname', width: 200, align: 'center', title: '备注' },
                { field: 'created', width: 200, align: 'center', title: '创建时间' },
                { field: 'status', align: 'center', width: 120, templet: '#columnStatus', title: '状态' },
                { align: 'center', width: 150, toolbar: '#tableBar', title: '操作' },
            ]]
        })
    })
</script>